<template>
  <!-- 角色表单弹窗 -->
  <el-dialog v-model="dialog.visible" :title="title" width="500px" @close="handleCloseDialog">
    <el-form
      v-if="dialog.visible"
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      class="form"
    >
      <el-form-item label="用户" prop="merchantUserId">
        <el-select
          v-model="formData.merchantUserId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入手机号搜索"
          :remote-method="remoteMethod"
          :loading="loading"
        >
          <el-option
            v-for="item in options"
            :key="item.userId"
            :label="item.nickname"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          placeholder="请输入备注"
          autosize
          type="textarea"
          clearable
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="handleCloseDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ElMessage } from "element-plus";
import PartnerUser from "@/api/system/partner-user";
// 定义 emit，用于通知外部关闭事件
const emit = defineEmits(["update:modelValue", "loadData"]);
// 弹窗
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
  defaultRowId: {
    type: String,
    default: "",
  },
  data: {
    type: Object,
  },
  id: {},
});

const dialog = reactive({
  title: "",
  visible: false,
});
const formRef = ref();
const loading = ref(false);
const options = ref([]);

const defaultFormData = {
  merchantUserId: "",
  remark: "",
};
const formData = ref({
  ...defaultFormData,
});
const formRules = reactive({
  merchantUserId: [{ required: true, message: "请选择用户", trigger: "change" }],
});
const title = computed(() => {
  return formData.value.id ? "编辑" : "新增";
});
const isEdit = computed(() => {
  return !!formData.value.id;
});

// 监听外部传入的 modelValue，更新内部 visible 状态
watch(
  () => props.modelValue,
  (newValue) => {
    dialog.visible = newValue;
    if (newValue) {
      formData.value = {
        ...defaultFormData,
      };
    }
  }
);

async function handleSubmit() {
  const validate = await formRef.value.validate();
  if (!validate) return;
  try {
    await PartnerUser.add(formData.value);
    ElMessage.success("成功");
    emit("loadData");
    handleCloseDialog();
  } catch (error) {
    console.log("error", error);
  }
}

const remoteMethod = async (query: string) => {
  if (query) {
    try {
      loading.value = true;
      const data = await PartnerUser.getPartnerByMobile({
        mobile: query,
      });
      options.value = (data && [data]) || [];
    } catch (e) {
      console.log(e);
    } finally {
      loading.value = false;
    }
  } else {
    options.value = [];
  }
};

// 关闭弹窗
function handleCloseDialog() {
  dialog.visible = false;
  emit("update:modelValue", false); // 关闭弹窗
}
</script>
<style style="scss" scoped>
::v-deep(.el-tabs__content) {
  min-height: 100px;
  display: flex;
  align-items: center;
}
</style>
